<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="80px">
      <el-form-item label="申请人" prop="applicantName">
        <el-input v-model="queryParams.applicantName" placeholder="请输入申请人姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.sys_repair_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="维修项目" prop="repairItem">
        <el-input v-model="queryParams.repairItem" placeholder="请输入维修项目" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table v-loading="loading" :data="repairList">
      <el-table-column prop="id" label="ID" align="center" />
      <el-table-column prop="applicantId" label="申请人ID" align="center" />
      <el-table-column prop="applicantName" label="申请人姓名" align="center" />
      <el-table-column prop="repairItem" label="维修项目" align="center" />
      <el-table-column prop="submitTime" label="提交时间" align="center">
        <template slot-scope="scope">{{ parseTime(scope.row.submitTime) }}</template>
      </el-table-column>
      <el-table-column prop="status" label="状态" align="center">
        <template slot-scope="scope">
          {{ dict.label.sys_repair_status[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column prop="assigneeName" label="维修人" align="center" />
      <el-table-column prop="completedTime" label="完成时间" align="center">
        <template slot-scope="scope">{{ parseTime(scope.row.completedTime) }}</template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="viewDetail(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 详情弹窗 -->
    <el-dialog title="维修单详情" :visible.sync="detailVisible" width="800px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="ID">{{ detail.id }}</el-descriptions-item>
        <el-descriptions-item label="房产ID">{{ detail.propertyId }}</el-descriptions-item>
        <el-descriptions-item label="房产地址">{{ detail.propertyAddress }}</el-descriptions-item>
        <el-descriptions-item label="申请人ID">{{ detail.applicantId }}</el-descriptions-item>
        <el-descriptions-item label="申请人">{{ detail.applicantName }}</el-descriptions-item>
        <el-descriptions-item label="联系电话">{{ detail.applicantPhone }}</el-descriptions-item>
        <el-descriptions-item label="维修项目">{{ detail.repairItem }}</el-descriptions-item>
        <el-descriptions-item label="维修描述">{{ detail.repairDescription }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          {{ dict.label.sys_repair_status[detail.status] }}
        </el-descriptions-item>
        <el-descriptions-item label="图片">
          <img :src="getFullUrl(detail.imageUrl)" v-if="detail.imageUrl" style="width: 100px; height: auto;" />
        </el-descriptions-item>
        <el-descriptions-item label="提交时间">{{ parseTime(detail.submitTime) }}</el-descriptions-item>
        <el-descriptions-item label="维修人">{{ detail.assigneeName }}</el-descriptions-item>
        <el-descriptions-item label="维修人电话">{{ detail.assigneePhone }}</el-descriptions-item>
        <el-descriptions-item label="完成时间">{{ parseTime(detail.completedTime) }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>


  </div>
</template>

<script>
import { listRepair, getRepair, updateRepair, delRepair } from "@/api/system/repair";

export default {
  name: "RepairList",
  dicts: ['sys_repair_status'],
  data() {
    return {
      loading: false,
      repairList: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applicantName: undefined,
        status: undefined,
        repairItem: undefined
      },
      detailVisible: false,
      dealVisible: false,
      detail: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getFullUrl(url) {
      console.log("url:" + url)
      if (!url) return ''; // 防止空值
      return process.env.VUE_APP_BASE_API + url;
    },
    getList() {
      this.loading = true;
      listRepair(this.queryParams).then(res => {
        this.repairList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams = { pageNum: 1, pageSize: 10 };
      this.getList();
    },
    viewDetail(row) {
      getRepair(row.id).then(res => {
        this.detail = res.data;
        this.detailVisible = true;
      });
    },
    markAsDealt(row) {
      this.dealForm = {
        id: row.id,
        assigneeName: row.assigneeName || '',
        assigneePhone: row.assigneePhone || ''
      };
      this.dealVisible = true;
    },
    submitDeal() {
      this.$refs.dealForm.validate(valid => {
        if (!valid) return;
        const payload = {
          id: this.dealForm.id,
          assigneeName: this.dealForm.assigneeName,
          assigneePhone: this.dealForm.assigneePhone,
          status: '1'
        };
        updateRepair(payload).then(() => {
          this.$message.success('维修单处理成功');
          this.dealVisible = false;
          this.getList();
        });
      });
    },
    handleDelete(row) {
      this.$confirm('确定要删除该维修单吗？', '提示', { type: 'warning' }).then(() => {
        delRepair(row.id).then(() => {
          this.$message.success('删除成功');
          this.getList();
        });
      });
    }
  }
};
</script>
